#include "SDLWindow.h"

// 怎么显示窗口呢？
// 1.窗口->内存空间 -（渲染）->显卡 ->显示器(大概流程)
void firstSDL()
{
    SDL_Window* window = NULL;
    SDL_Init(SDL_INIT_VIDEO);

    window = SDL_CreateWindow("SDL window",
                              SDL_WINDOWPOS_UNDEFINED,
                              SDL_WINDOWPOS_UNDEFINED,
                              600,400,
                              SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS);

    if(!window)
    {
        printf("failed create window\n");
        goto _EXIT;
    }
    SDL_Delay(2000);
    SDL_DestroyWindow(window);
_EXIT:
    SDL_Quit();
}


int renderWindow()
{

    SDL_Window* window = NULL;
    SDL_Renderer* render;

    SDL_Init(SDL_INIT_VIDEO);

    window = SDL_CreateWindow("SDL window",
                              SDL_WINDOWPOS_UNDEFINED,
                              SDL_WINDOWPOS_UNDEFINED,
                              600,400,
                              SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS);

    if(!window)
    {
        printf("failed create window\n");
        goto _EXIT;
    }

    render = SDL_CreateRenderer(window,-1,0);
    if(!render)
    {
        printf("failed create render\n");
        goto _DWINDOW;
    }

    // 为什么set 在clear之后不生效？
    SDL_SetRenderDrawColor(render,255,0,0,255);
    // 清屏
    SDL_RenderClear(render);

    // 显示
    SDL_RenderPresent(render);

    SDL_Delay(5000);
_DWINDOW:
    SDL_DestroyWindow(window);
_EXIT:
    SDL_Quit();

    return 0;
}

int sdlEvent()
{
    int quit = 1;
    SDL_Window* window = NULL;
    SDL_Renderer* render;
    SDL_Event event;

    SDL_Init(SDL_INIT_VIDEO);

    window = SDL_CreateWindow("SDL window",
                              SDL_WINDOWPOS_UNDEFINED,
                              SDL_WINDOWPOS_UNDEFINED,
                              600,400,
                              SDL_WINDOW_SHOWN);

    if(!window)
    {
        printf("failed create window\n");
        goto _EXIT;
    }

    render = SDL_CreateRenderer(window,-1,0);
    if(!render)
    {
        printf("failed create render\n");
        goto _DWINDOW;
    }

    // 为什么set 在clear之后不生效？
    SDL_SetRenderDrawColor(render,255,0,0,255);
    // 清屏,刷子？
    SDL_RenderClear(render);

    // 显示
    SDL_RenderPresent(render);

    do
    {
        SDL_WaitEvent(&event);

        switch (event.type) {
        case SDL_QUIT:
            quit = 0;
            break;
        default:
            SDL_Log("evnet type is %d",event.type);
            break;
        }
    }
    while(quit);
_DWINDOW:
    SDL_DestroyWindow(window);
_EXIT:
    SDL_Quit();

    return 0;
}

int sdlTexture()
{
    int quit = 1;
    SDL_Window* window = NULL;
    SDL_Renderer* render =NULL;
    SDL_Event event;
    SDL_Texture* texture = NULL;
    SDL_Rect rect;
    rect.w = 30;
    rect.h = 30;

    SDL_Init(SDL_INIT_VIDEO);

    window = SDL_CreateWindow("SDL window",
                              SDL_WINDOWPOS_UNDEFINED,
                              SDL_WINDOWPOS_UNDEFINED,
                              600,400,
                              SDL_WINDOW_SHOWN);

    if(!window)
    {
        printf("failed create window\n");
        goto _EXIT;
    }

    render = SDL_CreateRenderer(window,-1,0);
    if(!render)
    {
        printf("failed create render\n");
        goto _DWINDOW;
    }

    texture = SDL_CreateTexture(render,
                      SDL_PIXELFORMAT_RGBA8888,
                      SDL_TEXTUREACCESS_TARGET,
                      640,
                      480);
    if(!texture)
    {
        printf("failed create texture\n");
        goto _RENDER;
    }

    do
    {
        //SDL_WaitEvent(&event);
        SDL_PollEvent(&event);  // 记得用这个，不用程序崩溃

        switch (event.type) {
        case SDL_QUIT:
            quit = 0;
            break;
        default:
            SDL_Log("evnet type is %d",event.type);
            break;
        }

        rect.x = rand() % 600;
        rect.y = rand() % 450;

        SDL_SetRenderTarget(render,texture);
        SDL_SetRenderDrawColor(render,0,0,0,0);
        SDL_RenderClear(render);

        SDL_RenderDrawRect(render,&rect);
        SDL_SetRenderDrawColor(render,255,0,0,0);
        SDL_RenderFillRect(render,&rect);

        SDL_SetRenderTarget(render,NULL); // 默认目标
        SDL_RenderCopy(render,texture,NULL,NULL);

        SDL_RenderPresent(render);
    }
    while(quit);


    SDL_DestroyTexture(texture);
_RENDER:
    SDL_DestroyRenderer(render);

_DWINDOW:
    SDL_DestroyWindow(window);
_EXIT:
    SDL_Quit();
    return 0;
}
